package cn.iocoder.yudao.module.blog.controller.admin.ask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.ask.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.ask.BlogAskDO;
import cn.iocoder.yudao.module.blog.convert.ask.BlogAskConvert;
import cn.iocoder.yudao.module.blog.service.ask.BlogAskService;

@Tag(name = "管理后台 - 学员提问")
@RestController
@RequestMapping("/blog/ask")
@Validated
public class BlogAskController {

    @Resource
    private BlogAskService askService;

    @PostMapping("/create")
    @Operation(summary = "创建学员提问")
    @PreAuthorize("@ss.hasPermission('blog:ask:create')")
    public CommonResult<Long> createAsk(@Valid @RequestBody BlogAskCreateReqVO createReqVO) {
        return success(askService.createAsk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学员提问")
    @PreAuthorize("@ss.hasPermission('blog:ask:update')")
    public CommonResult<Boolean> updateAsk(@Valid @RequestBody BlogAskUpdateReqVO updateReqVO) {
        askService.updateAsk(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学员提问")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:ask:delete')")
    public CommonResult<Boolean> deleteAsk(@RequestParam("id") Long id) {
        askService.deleteAsk(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学员提问")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:ask:query')")
    public CommonResult<BlogAskRespVO> getAsk(@RequestParam("id") Long id) {
        BlogAskDO ask = askService.getAsk(id);
        return success(BlogAskConvert.INSTANCE.convert(ask));
    }

    @GetMapping("/list")
    @Operation(summary = "获得学员提问列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:ask:query')")
    public CommonResult<List<BlogAskRespVO>> getAskList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogAskDO> list = askService.getAskList(ids);
        return success(BlogAskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学员提问分页")
    @PreAuthorize("@ss.hasPermission('blog:ask:query')")
    public CommonResult<PageResult<BlogAskRespVO>> getAskPage(@Valid BlogAskPageReqVO pageVO) {
        PageResult<BlogAskDO> pageResult = askService.getAskPage(pageVO);
        return success(BlogAskConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学员提问 Excel")
    @PreAuthorize("@ss.hasPermission('blog:ask:export')")
    @OperateLog(type = EXPORT)
    public void exportAskExcel(@Valid BlogAskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogAskDO> list = askService.getAskList(exportReqVO);
        // 导出 Excel
        List<BlogAskExcelVO> datas = BlogAskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "学员提问.xls", "数据", BlogAskExcelVO.class, datas);
    }

}
